﻿Imports System.Data
Imports System.Data.SqlClient

Public Class tiposCargasDAL

    Sub New()

    End Sub



    Function inserir(ByVal tcDescricao As String,
                     ByVal tcAtivo As Char) As Integer
        Dim cmd As New SqlCommand
        Dim objD As New Dados
        Dim codigo As Integer
        Try

            'Comando para inserção de dados no banco
            cmd.CommandText = "INSERT INTO tipos_cargas (tc_descricao, tc_ativo_sn) values (@tcDescricao, @tcAtivo)"

            'Parâmetros de entrada
            cmd.Parameters.Add(New SqlParameter("@tcDescricao", SqlDbType.VarChar, 50)).Value = tcDescricao
            cmd.Parameters.Add(New SqlParameter("@tcAtivo", SqlDbType.Char)).Value = tcAtivo

            'Execução da inserção com retorno do valor da chave primária
            codigo = objD.executacomandoAI(cmd, "tipos_cargas")

        Catch ex As Exception
            Throw ex
        End Try
        cmd = Nothing
        objD = Nothing
        Return codigo
    End Function

    Sub alterar(ByVal codigo As Integer, ByVal tcDescricao As String,
                     ByVal tcAtivo As Char)
        Dim cmd As New SqlCommand
        Dim objD As New Dados

        Try

            'Comando para atualização de dados no banco
            cmd.CommandText = "UPDATE tipos_cargas SET tc_descricao = @tcDescricao," & _
                " tc_ativo_sn = @tcAtivo" & _
                " WHERE tc_codigo = @codigo"

            'Parâmetros de entrada
            cmd.Parameters.Add(New SqlParameter("@codigo", SqlDbType.Int)).Value = codigo
             cmd.Parameters.Add(New SqlParameter("@tcDescricao", SqlDbType.VarChar, 50)).Value = tcDescricao
            cmd.Parameters.Add(New SqlParameter("@tcAtivo", SqlDbType.Char)).Value = tcAtivo

            'Execução da alteração
            objD.executacomando(cmd)
        Catch ex As Exception
            Throw ex
        End Try
        cmd = Nothing
        objD = Nothing
    End Sub

    Sub excluir(ByVal codigo As Integer)
        Dim cmd As New SqlCommand
        Dim objD As New Dados
        Try
            'Comando para exclusão de um registro no banco
            cmd.CommandText = "DELETE FROM tipos_cargas WHERE tc_codigo = @codigo"

            'Parâmetro de comparação
            cmd.Parameters.Add(New SqlParameter("@codigo", SqlDbType.Int)).Value = codigo

            'Execução do comando
            objD.executacomando(cmd)
        Catch ex As Exception
            Throw ex
        End Try
        cmd = Nothing
        objD = Nothing
    End Sub

    Function localizar(ByVal descricao As String) As DataTable
        Dim cmd As New SqlCommand
        Dim objD As New Dados
        Dim tab As DataTable = Nothing
        Try
            If (descricao = "ultimo") Then
                cmd.CommandText = "SELECT tc_codigo = max(tc_codigo) FROM tipos_cargas "
            ElseIf (descricao = "primeiro") Then
                cmd.CommandText = "SELECT tc_codigo = min(tc_codigo) FROM tipos_cargas "

            Else
                'Comando para localizar vários registros no banco
                cmd.CommandText = "SELECT * FROM tipos_cargas WHERE tc_descricao like @descricao"

                'Parâmetro de entrada
                cmd.Parameters.Add(New SqlParameter("@descricao", SqlDbType.VarChar)).Value = descricao + "%"
            End If
            'Execução do comando
            tab = objD.executaConsulta(cmd)

        Catch ex As Exception
            Throw ex
        End Try
        cmd = Nothing
        objD = Nothing
        Return tab
        tab = Nothing
    End Function

    Function localizarProxAnterior(ByVal descricao As String, ByVal codigo As Integer) As DataTable
        Dim cmd As New SqlCommand
        Dim objD As New Dados
        Dim tab As DataTable = Nothing
        Try
            If (descricao = "proximo") Then
                cmd.CommandText = "SELECT tc_codigo FROM tipos_cargas WHERE tc_codigo = (SELECT MIN(tc_codigo) FROM tipos_cargas WHERE tc_codigo > @codigo)"

            ElseIf (descricao = "anterior") Then
                cmd.CommandText = "SELECT tc_codigo FROM tipos_cargas WHERE tc_codigo = (SELECT MAX(tc_codigo) FROM tipos_cargas WHERE tc_codigo < @codigo)"


            End If
            'Parâmetro de entrada
            cmd.Parameters.Add(New SqlParameter("@codigo", SqlDbType.Int)).Value = codigo
            'Execução do comando
            tab = objD.executaConsulta(cmd)

        Catch ex As Exception
            Throw ex
        End Try
        cmd = Nothing
        objD = Nothing
        Return tab
        tab = Nothing
    End Function



    Function localizar(ByVal codigo As Integer) As DataTable
        Dim cmd As New SqlCommand
        Dim objD As New Dados
        Dim tab As DataTable = Nothing
        Try
            'Comando para localizar vários registros no banco
            cmd.CommandText = "SELECT * FROM tipos_cargas WHERE tc_codigo = @codigo"

            'Parâmetro de entrada 
            cmd.Parameters.Add(New SqlParameter("@codigo", SqlDbType.Int)).Value = codigo

            'Execução do comando
            tab = objD.executaConsulta(cmd)

        Catch ex As Exception
            Throw ex
        End Try
        cmd = Nothing
        objD = Nothing
        Return tab
        tab = Nothing
    End Function

    Function VerificaExclusao(ByVal codigo As Integer) As Boolean
        Dim cmd As New SqlCommand
        Dim objD As New Dados
        Dim verifica As Boolean = False
        Try
            cmd.CommandText = "Select tc_codigo from cargas where tc_codigo = @codigo"
            cmd.Parameters.Add(New SqlParameter("@codigo", SqlDbType.Int)).Value = codigo
            verifica = objD.executacomandoString(cmd, "cargas")
        Catch ex As Exception
            Throw ex
        End Try
        cmd = Nothing
        objD = Nothing
        Return verifica
    End Function

    Function VerificaSituacao(ByVal codigo As String) As Boolean
        Dim cmd As New SqlCommand
        Dim objD As New Dados
        Dim verifica As Boolean = False
        Try
            cmd.CommandText = "Select tc_codigo from cargas where tc_codigo = @codigo and car_status <> 4 and car_status <> 7"
            cmd.Parameters.Add(New SqlParameter("@codigo", SqlDbType.Int)).Value = codigo
            verifica = objD.executacomandoString(cmd, "cargas")
        Catch ex As Exception
            Throw ex
        End Try
        cmd = Nothing
        objD = Nothing
        Return verifica
    End Function

End Class
